package libraryManagerPanels;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import java.awt.*;
import org.jfree.ui.*;

import clientGUI.MainGUI;
import clientGUI.PanelDesign;

import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.HashMap;

import javax.swing.JComboBox;

public class ReportsPanel extends PanelDesign  {  //PanelDesign 
		/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static JTextField numActiv;
	private static JTextField numfrozen;
	private static JTextField numNotActiv;
	private static JTextField numBorrowed;
	private static JTextField numLate;
	private static JTextField popAvg;
	private static JTextField popMedian;
	private static JTextField regAvg;
	private static JTextField regMedian;
	private static JTextField lateAvg;
	private static JTextField lateMedian;
	private int chooseYearBorrow=0 ;
	private int chooseYearLates=0 ;
	
	private static Double[] pop;
	private static Double[] reg;

	private static HashMap<String,Double> lateDuration;
	private static HashMap<String,Double> numDelays ;
	private static String[] bookNames ; 

	
		public ReportsPanel()
        {
			setLayout(null);
			setPreferredSize(new Dimension(644, 462));

			JPanel activity=new JPanel();
			JPanel late=new JPanel();

			JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
			tabbedPane.setBounds(3, 3, 638, 456);
			
			tabbedPane.addTab("Activities Report", activity);
			activity.setLayout(null);
			
			JPanel panel_2 = new JPanel();
			panel_2.setBounds(170, 65, 297, 188);
			activity.add(panel_2);
			panel_2.setLayout(null);
			
			JLabel lblNumberOfActivities = new JLabel("Number Of  Activities Subscribers:");
			lblNumberOfActivities.setBounds(0, 0, 211, 22);
			panel_2.add(lblNumberOfActivities);
			lblNumberOfActivities.setFont(new Font("Tahoma", Font.PLAIN, 13));
			
			JLabel lblNewLabel = new JLabel("Number Of  Frozen Subscribers:");
			lblNewLabel.setBounds(0, 40, 211, 21);
			panel_2.add(lblNewLabel);
			lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 13));
			
			JLabel lblNewLabel_1 = new JLabel("Number Of  Inactive Subscribers:");
			lblNewLabel_1.setBounds(0, 80, 211, 22);
			panel_2.add(lblNewLabel_1);
			lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 13));
			
			JLabel lblNewLabel_2 = new JLabel("Number Of  Borrowed Copies:");
			lblNewLabel_2.setBounds(0, 120, 211, 22);
			panel_2.add(lblNewLabel_2);
			lblNewLabel_2.setFont(new Font("Tahoma", Font.PLAIN, 13));
			
			JLabel lblNewLabel_3 = new JLabel("Number Of  Lates In Return:");
			lblNewLabel_3.setBounds(0, 160, 211, 28);
			panel_2.add(lblNewLabel_3);
			lblNewLabel_3.setFont(new Font("Tahoma", Font.PLAIN, 13));
			
			numActiv = new JTextField();
			numActiv.setBounds(211, 2, 86, 20);
			panel_2.add(numActiv);
			numActiv.setEditable(false);
			numActiv.setColumns(10);
			
			numfrozen = new JTextField();
			numfrozen.setBounds(211, 41, 86, 20);
			panel_2.add(numfrozen);
			numfrozen.setEditable(false);
			numfrozen.setText("");
			numfrozen.setColumns(10);
			
			numNotActiv = new JTextField();
			numNotActiv.setBounds(211, 82, 86, 20);
			panel_2.add(numNotActiv);
			numNotActiv.setEditable(false);
			numNotActiv.setColumns(10);
			
			numBorrowed = new JTextField();
			numBorrowed.setBounds(211, 122, 86, 20);
			panel_2.add(numBorrowed);
			numBorrowed.setEditable(false);
			numBorrowed.setColumns(10);
			
			numLate = new JTextField();
			numLate.setBounds(211, 165, 86, 20);
			panel_2.add(numLate);
			numLate.setEditable(false);
			numLate.setColumns(10);
			
			JLabel lblNewLabel_4 = new JLabel("Activities Report:");
			lblNewLabel_4.setFont(new Font("Tahoma", Font.BOLD, 14));
			lblNewLabel_4.setBounds(231, 23, 196, 28);
			activity.add(lblNewLabel_4);
			
			
			
			JPanel borrow=new JPanel();
			
			tabbedPane.addTab("Borrowed Books Report", borrow);
			borrow.setLayout(null);
			
			JLabel lblNewLabel_5 = new JLabel("Time Of  Borrowed Books Report:");
			lblNewLabel_5.setFont(new Font("Tahoma", Font.BOLD, 14));
			lblNewLabel_5.setBounds(188, 36, 271, 31);
			borrow.add(lblNewLabel_5);
			
			JPanel panel = new JPanel();
			panel.setBounds(50, 78, 532, 158);
			borrow.add(panel);
			panel.setLayout(null);
			
			JLabel lblAverage = new JLabel("Popular Books Average:");
			lblAverage.setBounds(0, 36, 142, 25);
			panel.add(lblAverage);
			lblAverage.setFont(new Font("Tahoma", Font.PLAIN, 13));
			
			JLabel lblNewLabel_6 = new JLabel("Popular Books Median:");
			lblNewLabel_6.setBounds(0, 72, 142, 18);
			panel.add(lblNewLabel_6);
			lblNewLabel_6.setFont(new Font("Tahoma", Font.PLAIN, 13));
			
			popAvg = new JTextField();
			popAvg.setBounds(152, 39, 86, 20);
			panel.add(popAvg);
			popAvg.setEditable(false);
			popAvg.setColumns(10);
			
			popMedian = new JTextField();
			popMedian.setBounds(152, 72, 86, 20);
			panel.add(popMedian);
			popMedian.setEditable(false);
			popMedian.setColumns(10);
			
			JLabel lblRegularBooksAverage = new JLabel("Regular Books Average:");
			lblRegularBooksAverage.setBounds(289, 36, 147, 25);
			panel.add(lblRegularBooksAverage);
			lblRegularBooksAverage.setFont(new Font("Tahoma", Font.PLAIN, 13));
			
			JLabel lblNewLabel_7 = new JLabel("Regular Books Median:");
			lblNewLabel_7.setBounds(289, 73, 147, 17);
			panel.add(lblNewLabel_7);
			lblNewLabel_7.setFont(new Font("Tahoma", Font.PLAIN, 13));
			
			regAvg = new JTextField();
			regAvg.setBounds(446, 39, 86, 20);
			panel.add(regAvg);
			regAvg.setEditable(false);
			regAvg.setColumns(10);
			
			regMedian = new JTextField();
			regMedian.setBounds(446, 72, 86, 20);
			panel.add(regMedian);
			regMedian.setEditable(false);
			regMedian.setColumns(10);
			
			JButton showHisto = new JButton("Show Borrow Diagram");
			showHisto.setBounds(203, 122, 147, 36);
			panel.add(showHisto);
			
			
			final JComboBox<Integer> comboBoxBorrow = new JComboBox<Integer>();
			comboBoxBorrow.setBounds(262, 0, 71, 25);
			panel.add(comboBoxBorrow);
			comboBoxBorrow.setModel(new DefaultComboBoxModel<Integer>(new Integer[] {2012, 2013}));
			comboBoxBorrow.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					Integer year=(Integer) comboBoxBorrow.getSelectedItem();
					chooseYearBorrow=1;
					String[] msg=new String[3];
					msg[0]="Reports";
					msg[1]="Borrowed";
					msg[2]=year.toString();
					MainGUI.OBLClient.handleMessageFromClient(msg);
				}
			});
			
			comboBoxBorrow.setSelectedIndex(1);
			
			JLabel lblSelectYear_1 = new JLabel("Select Year:");
			lblSelectYear_1.setBounds(166, 0, 86, 28);
			panel.add(lblSelectYear_1);
			lblSelectYear_1.setFont(new Font("Tahoma", Font.PLAIN, 13));
			showHisto.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(chooseYearBorrow==0) 
					{
						JOptionPane.showMessageDialog(new JFrame(),	"You need to choose a year");
						return;
					}
					BarChart x=new BarChart("Borrow Diagram",pop,reg);
			        x.pack();
			        RefineryUtilities.centerFrameOnScreen(x);
			        x.setVisible(true);		
				}
			});
			
			
			tabbedPane.addTab("Lates Return Report", late);
			late.setLayout(null);
			
			JLabel lblNewLabel_8 = new JLabel("Lates Of  Return Books Report:");
			lblNewLabel_8.setBounds(184, 30, 269, 27);
			lblNewLabel_8.setFont(new Font("Tahoma", Font.BOLD, 14));
			late.add(lblNewLabel_8);
			
			JPanel panel_1 = new JPanel();
			panel_1.setBounds(213, 69, 154, 85);
			late.add(panel_1);
			panel_1.setLayout(null);
			
			JLabel lblNewLabel_9 = new JLabel("Average:");
			lblNewLabel_9.setBounds(0, 32, 57, 21);
			panel_1.add(lblNewLabel_9);
			lblNewLabel_9.setFont(new Font("Tahoma", Font.PLAIN, 13));
			
			JLabel lblNewLabel_10 = new JLabel("Median:");
			lblNewLabel_10.setBounds(0, 64, 51, 21);
			panel_1.add(lblNewLabel_10);
			lblNewLabel_10.setFont(new Font("Tahoma", Font.PLAIN, 13));
			
			lateAvg = new JTextField();
			lateAvg.setBounds(68, 33, 86, 20);
			panel_1.add(lateAvg);
			lateAvg.setEditable(false);
			lateAvg.setColumns(10);
			
			lateMedian = new JTextField();
			lateMedian.setBounds(68, 65, 86, 20);
			panel_1.add(lateMedian);
			lateMedian.setEditable(false);
			lateMedian.setColumns(10);
			
			final JComboBox<Integer> comboBoxLates = new JComboBox<Integer>();
			comboBoxLates.setBounds(83, 1, 57, 21);
			panel_1.add(comboBoxLates);
			comboBoxLates.setModel(new DefaultComboBoxModel<Integer>(new Integer[] {2012, 2013}));
			comboBoxLates.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					Integer year=(Integer) comboBoxLates.getSelectedItem();
					chooseYearLates=1;
					String[] msg=new String[2];
					msg[0]="Reports";
					msg[1]=year.toString();
					MainGUI.OBLClient.handleMessageFromClient(msg);
				}
			});
			comboBoxLates.setSelectedIndex(1);  
			
			JLabel lblSelectYear = new JLabel("Select Year:");
			lblSelectYear.setBounds(0, 0, 86, 21);
			panel_1.add(lblSelectYear);
			lblSelectYear.setFont(new Font("Tahoma", Font.PLAIN, 13));
			
			JButton btnShowLatesDiagram = new JButton("Show Lates Diagram");
			btnShowLatesDiagram.setBounds(213, 182, 141, 35);
			btnShowLatesDiagram.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(chooseYearLates==0) 
					{
						JOptionPane.showMessageDialog(new JFrame(),	"You need to choose a year");
						return;
					}
					BarChart x=new BarChart("Lates Diagram",lateDuration,numDelays,bookNames);
			        x.pack();
			        RefineryUtilities.centerFrameOnScreen(x);
			        x.setVisible(true);	
					
				}
			});
			
			late.add(btnShowLatesDiagram);
			
			add(tabbedPane);
			
			String[] msg=new String[2];
			msg[0]="Reports";
			msg[1]="Activity" ; 
			MainGUI.OBLClient.handleMessageFromClient(msg);
			
			
			remove(background);
			JLabel x=new JLabel();
			x.setIcon(background.getIcon());
			x.setBounds(new Rectangle(0, 0, 638, 456));
			JLabel y=new JLabel();
			y.setIcon(background.getIcon());
			y.setBounds(new Rectangle(0, 0, 638, 456));
			JLabel z=new JLabel();
			z.setIcon(background.getIcon());
			z.setBounds(new Rectangle(0, 0, 638, 456));
			activity.add(x);
			borrow.add(y);
			late.add(z);
			
			add(background);
        }
		
		
	/**
	 * Replaces the data of the fields in the first tab in this panel from the given Matrix.
	 * @param info
	 */	
		public static void updateFieldsActivity(String[] info)
		{
			
			numActiv.setText(info[0]);
			numNotActiv.setText(info[1]);
			numfrozen.setText(info[2]);
			numBorrowed.setText(info[3]);
			numLate.setText(info[4]);
			
		}
		
		
		/**
		 * Replaces the data of the fields in the second tab in this panel from the given Matrix.
		 * @param info
		 */		
		public static void updateFieldsBorrowed(String[] info)
		{
			
			popAvg.setText(info[0]);
			popMedian.setText(info[1]);	
			regAvg.setText(info[2]);
			regMedian.setText(info[3]);
			
		}
		
		/**
		 * Replaces the data of the fields in the third tab in this panel from the given Matrix.
		 * @param info
		 */		
		public static void updateFieldsLates(String[] info)
		{
			
			lateAvg.setText(info[0]);
			lateMedian.setText(info[1]);
			
		}
		
		
		/**
		 * Initialize the arrays in class.
		 * @param infoPop
		 * @param infoReg
		 */
		public static void setDataForHisto(Double[] infoPop, Double[] infoReg)
		{
			pop=infoPop;
			reg=infoReg;
		}
		
		
		
		/**
		 * Initialize Hash Maps and array   
		 * @param duration
		 * @param count
		 * @param names
		 */	
		public static void setDataForHisto(HashMap<String,Double> duration, HashMap<String,Double> count, String[] names )
		{
			lateDuration=duration;
			numDelays=count; 
			bookNames=names; 
		}
		
		
}
